package com.example.admistrator.myvideoplayer;

import android.content.res.Configuration;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Surface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.VideoView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.facebook.drawee.view.SimpleDraweeView;

public class VideoActivity extends AppCompatActivity implements OnClickListener {

  MediaController controller;
  @BindView(R.id.iv_first_frame)
  SimpleDraweeView mIvFirstFrame;
  @BindView(R.id.videoView)
  VideoView mVideoView;
  @BindView(R.id.progressBar)
  ContentLoadingProgressBar mProgressBar;
  @BindView(R.id.iv_play)
  ImageView mIvPlay;
  @BindView(R.id.re_play)
  FrameLayout mRePlay;
  private RelativeLayout.LayoutParams mMVideoViewLayoutParams;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);
    ButterKnife.bind(this);

    mIvFirstFrame.setImageURI(
        "http://bmob-cdn-7694.b0.upaiyun.com/2016/12/11/eba5790840501537803ad3644a828a7b.png");


    //显示播放图标
    mIvPlay.setVisibility(View.VISIBLE);
    mIvPlay.setOnClickListener(this);
    mProgressBar.setVisibility(View.GONE);

    mVideoView.setOnPreparedListener(new OnPreparedListener() {
      @Override
      public void onPrepared(MediaPlayer mp) {

        int width = mp.getVideoWidth();
        int height = mp.getVideoHeight();
        Rect rect = new Rect();
        mIvFirstFrame.getGlobalVisibleRect(rect);
        mIvFirstFrame.setImageURI(
            "http://bmob-cdn-7694.b0.upaiyun.com/2016/12/11/eba5790840501537803ad3644a828a7b.png");
        int nowWidth = rect.width();
        int nowHeight = nowWidth * height / width;
//        resizeView((View) mIvFirstFrame.getParent(), nowWidth, nowHeight);
//                    resizeView(mVideoView, 500, 200);

        Log.e("MainActivity123", "onPrepared=开始播放");
        mProgressBar.setVisibility(View.GONE);
        mIvFirstFrame.setVisibility(View.GONE);

        mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
          @Override
          public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
           /*
            *  add media controller
            */
            controller = new MediaController(VideoActivity.this);
            ;
            mVideoView.setMediaController(controller);
            /*
            * and set its position on screen
             */
            controller.setAnchorView(mVideoView);
          }
        });
      }
    });

    //播放完毕
    mVideoView.setOnCompletionListener(new OnCompletionListener() {
      @Override
      public void onCompletion(MediaPlayer mp) {
        mRePlay.setVisibility(View.VISIBLE);
        Log.e("MainActivity123", "onPrepared=开始完毕");

        //获取到的是最后一次看多进度条是的位置
        int currentPosition = mp.getCurrentPosition();

        //获取的是视频的总时间，单位ms
        int duration = mp.getDuration();

        //获取播放进度
        int bufferPercentage = mVideoView.getBufferPercentage();

        Log.e("MainActivity1  3", "onCreate=" + bufferPercentage + "=" + duration);

      }
    });

    mVideoView.start();
  }


  private static void resizeView(View view, int width, int height) {
    ViewGroup.LayoutParams layout = view.getLayoutParams();
    layout.width = width;
    layout.height = height;
    view.setLayoutParams(layout);
  }


  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.iv_play:
        Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();

        mIvPlay.setVisibility(View.GONE);
        mProgressBar.setVisibility(View.VISIBLE);
        mVideoView.setVisibility(View.VISIBLE);
        mVideoView.setVideoPath(
            "http://192.168.3.215/Public/proofvideo/2017-08-04/ab8464cfa9c09e21571c37223e8a0830.mp4");
        mVideoView.requestFocus();
        mVideoView.seekTo(0);
        mVideoView.start();
        break;
    }
  }


  @OnClick(R.id.re_play)
  protected void onClickRePlay() {
    mVideoView.seekTo(0);
    mVideoView.start();
    mIvPlay.setVisibility(View.GONE);
    mRePlay.setVisibility(View.GONE);
  }


  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int rot = getWindowManager().getDefaultDisplay().getRotation();
    if(rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270){
      mMVideoViewLayoutParams = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams();
      RelativeLayout.LayoutParams layoutParams =
          new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
      mVideoView.setLayoutParams(layoutParams);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    }else if(rot == Surface.ROTATION_0){
//            RelativeLayout.LayoutParams lp = new  RelativeLayout.LayoutParams(320,240);
//            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
      mVideoView.setLayoutParams(mMVideoViewLayoutParams);
    }
  }
}
